package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.ReqQryFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/30.
 */
public class CITIC02QryFinanceRecInfo extends CITIC02Base implements IFinanceRecInfo{
    @Override
    public RetAtsFinanceRecInfo qryFinanceRecInfo(ReqQryFinanceRecInfo recInfo) throws Exception {
        RetAtsFinanceRecInfo retAts = new RetAtsFinanceRecInfo(recInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            int page =1;
            int hasReadNum =0;
            int totalNum =0;
            do {
                CITIC02Header header = new CITIC02Header();
                header.stdprocode = "RLTMSG";
                header.msgsendertrc = reqHeader.transSeqID;
                header.trdcode = "CPCCSDPQ";
                //            封装body内容
                XmlTextWriter xmlWriterBody = new XmlTextWriter();
                xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                xmlWriterBody.writeStartElement("stream");
//            LONCPID	企业端经销商客户代码	VARCHAR	20	Y
                xmlWriterBody.writeElementString("LONCPID", recInfo.DealerCode);
//            BKCUSTCD	银行端借款企业ID	VARCHAR	20	N
                xmlWriterBody.writeElementString("BKCUSTCD", "");
//            LONCPNM	借款企业名称	VARCHAR	100	Y
                xmlWriterBody.writeElementString("LONCPNM", recInfo.DealerName);
//            OPBRHCD	经办分行编号	VARCHAR	10	N
                xmlWriterBody.writeElementString("OPBRHCD", "");
//            OPBRHNM	经办分行名称	VARCHAR	100	N
                xmlWriterBody.writeElementString("OPBRHNM", "");
//            OUTDT	出库日期/反馈日期	VARCHAR	30	N	"“YYYYMMDD”查出库信息时出库日期必填； 查异常车辆信息时反馈日期必填，t-1日日期。"
                xmlWriterBody.writeElementString("OUTDT", "");
//            QUERYTP	查询类型	VARCHAR	1	Y	1-出库 2-在库，3-异常车辆状态
                xmlWriterBody.writeElementString("QUERYTP", "1");
//                PAGESIZE	每页记录数	VARCHAR	10	Y
                xmlWriterBody.writeElementString("PAGESIZE", "200");
//                PAGENO	当前页码	VARCHAR	10	Y
                xmlWriterBody.writeElementString("PAGENO", (page++)+"");
//            FIELD1	属性1	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD1", "");
//            FIELD2	属性2	VARCHAR	60
                xmlWriterBody.writeElementString("FIELD2", "");
//            FIELD3	属性3	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD3", "");

                xmlWriterBody.writeEndDocument();
                String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(), header);
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/stream/RETCODE");
                        if ("AAAAAAA".equals(tmpRetCode)) {
                            if(totalNum==0){
                                tmpRespCode = "0";
                                tmpRespInfo = "获取成功";
                                totalNum =Integer.valueOf(tmpBkReader.getFirstText("/stream/TTLNUM"));
                            }
                            while (tmpBkReader.read("/stream/list/row")) {
                                hasReadNum++;
                                RetAtsFinanceRecInfoDetail p_retDetail = new RetAtsFinanceRecInfoDetail();
//                            CHASNO	车架号	VARCHAR	30	Y	查在库车辆信息\出库车辆\异常车辆信息时必填
                                p_retDetail.VIN = tmpBkReader.readedXElement.getSingleText("CHASNO");
                                p_retDetail.DealerCode = recInfo.DealerCode;
//                            PLDNAME	押品名称	VARCHAR	80	N	车型
//                            PLDPRICE	押品单价	DECIMAL	19,2	Y	"车价；
//                            查在库车辆信息和出库车辆信息时必填"
//                            PLDUNIT	押品单位	VARCHAR	2	Y	"1-份，2-克，3-公斤，4-吨，5-辆，默认为5-辆；
//                            查在库车辆信息和出库车辆信息时必填"
//                            HGZNO	合格证号	VARCHAR	60	N
                                p_retDetail.RepaymentMoney=tmpBkReader.readedXElement.getSingleText("PLDPRICE");
//                            INDT	入库日期	VARCHAR	30	Y	查在库车辆信息和出库车辆信息时必填
                                p_retDetail.CarMortgageTime = tmpBkReader.readedXElement.getSingleText("INDT");
                                p_retDetail.RepaymentDate = tmpBkReader.readedXElement.getSingleText("OUTDATE");
                                retAts.writeRetDetail(p_retDetail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpRetCode;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
            }while (hasReadNum<totalNum);
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
